/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language enrstuing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.enrstu.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.enrstu.service.ManageService;
import me.zhengjie.modules.enrstu.service.dto.ManageCreateOrUpdateDto;
import me.zhengjie.modules.enrstu.service.dto.ManageDto;
import me.zhengjie.modules.enrstu.service.dto.ManageQueryCriteria;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.resp.Result;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* @website https://eladmin.vip
* @author yyx
* @date 2024-10-14
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "招生系统：项目管理")
@RequestMapping("/api/enrstu/manage")
@Slf4j
public class ManageController {

    private final ManageService manageService;


    @GetMapping
    @Log("查询项目管理")
    @ApiOperation("查询项目管理")
    @PreAuthorize("@el.check('manage:list')")
    public ResponseEntity<PageResult<ManageDto>> queryManage(ManageQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(manageService.queryAll(criteria,pageable),HttpStatus.OK);
    }
    @GetMapping("/query/{id}")
    @Log("根据id查询项目管理")
    @ApiOperation("根据id查询项目管理")
    @PreAuthorize("@el.check('manage:list')")
    public ResponseEntity<ManageDto> queryManage(@PathVariable String id){
        return new ResponseEntity<>(manageService.findById(id),HttpStatus.OK);
    }

    @PostMapping("/createOrUpdate")
    @Log("新增或更新项目")
    @ApiOperation("新增或更新项目")
    @PreAuthorize("@el.check('manage:add') And @el.check('manage:edit')")
    public ResponseEntity<Object> createOrUpdateMg(@Validated @RequestBody ManageCreateOrUpdateDto resources){
        manageService.createOrUpdateMg(resources);
        return new ResponseEntity<>(Result.ok(),HttpStatus.CREATED);
    }



    @DeleteMapping("/{id}")
    @Log("删除项目管理")
    @ApiOperation("删除项目管理")
    @PreAuthorize("@el.check('manage:del')")
    public ResponseEntity<Object> deleteManage(@PathVariable String id) {
        manageService.deleteAll(id);
        return new ResponseEntity<>(Result.ok(),HttpStatus.OK);
    }


    @PutMapping("/revocation/{id}")
    @Log("撤销项目管理")
    @ApiOperation("撤销项目管理")
    @PreAuthorize("@el.check('manage:revoca')")
    public ResponseEntity<Object> revocationManage(@PathVariable String id) {
       manageService.revocationManage(id);
        return new ResponseEntity<>(Result.ok(),HttpStatus.OK);
    }

    @PutMapping("/start/{id}")
    @Log("启动项目管理")
    @ApiOperation("启动项目管理")
    @PreAuthorize("@el.check('manage:start')")
    public ResponseEntity<Object> startManage(@PathVariable String id) {
        manageService.startManage(id);
        return new ResponseEntity<>(Result.ok(),HttpStatus.OK);
    } 

    @GetMapping("/afflictionRange")
    @Log("获取所属年度范围")
    @ApiOperation("获取所属年度范围")
    @PreAuthorize("@el.check('manage:list')")
    public ResponseEntity<Object> getAfflictionRange(boolean isListAffRange){
        return new ResponseEntity<>(Result.ok().data("afflictionRange", manageService.getAfflictionRange(isListAffRange)),HttpStatus.CREATED);
    }
}